﻿using System;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.DuLieuChuan;
using System.Data;


namespace Company.Interface.GC
{
    public partial class DinhMucGCRegistedDetailForm : BaseForm
    {
        public DinhMuc DMDetail = new DinhMuc();
    
        public DinhMucGCRegistedDetailForm()
        {
            InitializeComponent();
        }
       
        private void txtMaNPL_Leave(object sender, EventArgs e)
        {
        //    NguyenPhuLieu nplSXXK = new NguyenPhuLieu();
        //    nplSXXK.MaHaiQuan = this.MaHaiQuan;
        //    nplSXXK.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
        //    nplSXXK.Ma = txtMaNPL.Text;
        //    if (nplSXXK.Load())
        //    {               
        //        txtMaNPL.Text = nplSXXK.Ma;
        //        txtTenNPL.Text = nplSXXK.Ten;
        //        txtMaHSNPL.Text = nplSXXK.MaHS;
        //        txtDonViTinhNPL.Text = DonViTinh.GetName(nplSXXK.DVT_ID);
        //        error.SetError(txtMaNPL, string.Empty);
        //    }
        //    else
        //    {                
        //        error.SetIconPadding(txtMaNPL, -8);
        //        error.SetError(txtMaNPL, "Không tồn tại nguyên phụ liệu này.");
                
        //        txtTenNPL.Text = txtMaHSNPL.Text = txtDonViTinhNPL.Text = string.Empty;
        //    }
        }

        private void txtMaSP_ButtonClick(object sender, EventArgs e)
        {
            //SanPhamRegistedForm f = new SanPhamRegistedForm();
            //f.CalledForm = this.Name;
            //f.ShowDialog(this);
            //if (f.SanPhamSelected.Ma.Length > 0)
            //{
            //    txtMaSP.Text = f.SanPhamSelected.Ma;
            //    txtTenSP.Text = f.SanPhamSelected.Ten;
            //    txtMaHSSP.Text = f.SanPhamSelected.MaHS;
            //    txtDonViTinhSP.Text = DonViTinh.GetName(f.SanPhamSelected.DVT_ID);
            //}
        }

        private void txtMaNPL_ButtonClick(object sender, EventArgs e)
        {
            //NguyenPhuLieuRegistedForm f = new NguyenPhuLieuRegistedForm();
            //f.CalledForm = this.Name;
            //f.ShowDialog(this);
            //if (f.NguyenPhuLieuSelected.Ma.Length > 0)
            //{
            //    txtMaNPL.Text = f.NguyenPhuLieuSelected.Ma;
            //    txtTenNPL.Text = f.NguyenPhuLieuSelected.Ten;
            //    txtMaHSNPL.Text = f.NguyenPhuLieuSelected.MaHS;
            //    txtDonViTinhNPL.Text = DonViTinh.GetName(f.NguyenPhuLieuSelected.DVT_ID);
            //}
        }

        private void DinhMucGCRegistedDetailForm_Load(object sender, EventArgs e)
        {
            SanPham sp = new SanPham();
            sp.Ma = DMDetail.MaSanPham;
            sp.SoHopDong = DMDetail.SoHopDong;
            sp.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            sp.MaHaiQuan = GlobalSettings.MA_HAI_QUAN;
            sp.NgayKy = DMDetail.NgayKy;
            sp.Load();
            txtMaSP.Text = sp.Ma;
            txtTenSP.Text = sp.Ten;
            txtMaHSSP.Text = sp.MaHS;
            txtDonViTinhSP.Text = this.DonViTinh_GetName(sp.DVT_ID);
            //load thong tin npl
            NguyenPhuLieu npl = new NguyenPhuLieu();
            npl.Ma = DMDetail.MaNguyenPhuLieu;
            npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            npl.MaHaiQuan = GlobalSettings.MA_HAI_QUAN;
            npl.NgayKy = DMDetail.NgayKy;
            npl.SoHopDong = DMDetail.SoHopDong;
            npl.Load();
            txtMaNPL.Text = npl.Ma;
            txtTenNPL.Text = npl.Ten;
            txtMaHSNPL.Text = npl.MaHS;
            txtDonViTinhNPL.Text = this.DonViTinh_GetName(npl.DVT_ID);
            txtDinhMuc.Text = DMDetail.DinhMucSuDung.ToString();
            txtTyLeHH.Text = DMDetail.TyLeHaoHut.ToString();
            txtNPLtucung.Text = DMDetail.NPL_TuCungUng.ToString();
            txtGhiChu.Text = DMDetail.GhiChu;
            dgList.DataSource = DMDetail.SelectCollectionBy_SoHopDong_AND_MaHaiQuan_AND_MaDoanhNghiep_AND_NgayKy();
        }

        private void txtMaSP_Leave(object sender, EventArgs e)
        {
            //SanPham spSXXK = new SanPham();
            //spSXXK.MaHaiQuan = this.MaHaiQuan;
            //spSXXK.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            //spSXXK.Ma = txtMaSP.Text;
            //if (spSXXK.Load())
            //{
            //    this.existSP = true;
            //    error.SetError(txtMaNPL, string.Empty);

            //    txtMaSP.Text = spSXXK.Ma;
            //    txtTenSP.Text = spSXXK.Ten;
            //    txtMaHSSP.Text = spSXXK.MaHS;
            //    txtDonViTinhSP.Text = DonViTinh.GetName(spSXXK.DVT_ID);
            //    error.SetError(txtMaSP, string.Empty);
            //}
            //else
            //{
            //    this.existSP = false;
            //    error.SetIconPadding(txtMaSP, -8);
            //    error.SetError(txtMaSP, "Không tồn tại sản phẩm này.");

            //    txtTenSP.Text = txtMaHSSP.Text = txtDonViTinhSP.Text = string.Empty;
            //}
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            //// 1. Kiểm tra dữ liệu.
            //cvError.Validate();
            //if (!cvError.IsValid) return;

            //if (!this.existNPL)
            //{
            //    error.SetIconPadding(txtMaNPL, -8);
            //    error.SetError(txtMaNPL, "Không tồn tại nguyên phụ liệu này.");
            //    return;
            //}
            //if (!this.existSP)
            //{
            //    error.SetIconPadding(txtMaSP, -8);
            //    error.SetError(txtMaSP, "Không tồn tại sản phẩm này.");
            //    return;
            //}

            //// 2. Cập nhật dữ liệu.
            //this.DMDetail.MaSanPham = txtMaSP.Text;
            //this.DMDetail.MaNguyenPhuLieu = txtMaNPL.Text;
            //this.DMDetail.DinhMucSuDung = Convert.ToDecimal(txtDinhMuc.Value);
            //this.DMDetail.TyLeHaoHut = Convert.ToDecimal(txtTyLeHH.Value);

            //// 3. Đóng.
            //this.Close();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.DMDetail = null;
            this.Close();
        }

        private void dgList_RowDoubleClick(object sender, Janus.Windows.GridEX.RowActionEventArgs e)
        {            
            txtMaSP.Text = e.Row.Cells[1].Text;
            txtMaNPL.Text = e.Row.Cells[2].Text;
            txtDinhMuc.Text = e.Row.Cells[3].Text;
            txtTyLeHH.Text = e.Row.Cells[4].Text;
            txtNPLtucung.Text = e.Row.Cells[5].Text;
            txtGhiChu.Text = e.Row.Cells[6].Text;
            //load thong tin sp
            SanPham sp = new SanPham();
            sp.Ma = txtMaSP.Text;
            sp.SoHopDong = DMDetail.SoHopDong;
            sp.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            sp.MaHaiQuan = DMDetail.MaHaiQuan;
            sp.NgayKy = DMDetail.NgayKy;
            sp.Load();
            txtTenSP.Text = sp.Ten;
            txtMaHSSP.Text = sp.MaHS;
            txtDonViTinhSP.Text = this.DonViTinh_GetName(sp.DVT_ID);

            //load thong tin npl
            NguyenPhuLieu npl = new NguyenPhuLieu();
            npl.Ma = txtMaNPL.Text;
            npl.MaDoanhNghiep = GlobalSettings.MA_DON_VI;
            npl.MaHaiQuan = DMDetail.MaHaiQuan;
            npl.NgayKy = DMDetail.NgayKy;
            npl.SoHopDong = DMDetail.SoHopDong;
            npl.Load();
            txtTenNPL.Text = npl.Ten;
            txtMaHSNPL.Text = npl.MaHS;
            txtDonViTinhNPL.Text = this.DonViTinh_GetName(npl.DVT_ID);          
        }
    }
}